using System;
using System.Collections.Generic;
using Volo.Abp.Domain.Entities.Auditing;

namespace CKY.AgentPlatform.Users
{
    /// <summary>
    /// 用户角色实体
    /// </summary>
    public class UserRole : FullAuditedAggregateRoot<Guid>
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public virtual Guid UserId { get; set; }

        /// <summary>
        /// 角色ID
        /// </summary>
        public virtual Guid RoleId { get; set; }

        /// <summary>
        /// 分配时间
        /// </summary>
        public virtual DateTime AssignedAt { get; set; }

        /// <summary>
        /// 分配者ID
        /// </summary>
        public virtual Guid? AssignedBy { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public virtual DateTime? ExpiresAt { get; set; }

        /// <summary>
        /// 是否已过期
        /// </summary>
        public virtual bool IsExpired => ExpiresAt.HasValue && ExpiresAt.Value < DateTime.UtcNow;

        protected UserRole()
        {
            // EF Core required constructor
        }

        public UserRole(
            Guid id,
            Guid userId,
            Guid roleId,
            Guid? assignedBy = null,
            DateTime? expiresAt = null)
            : base(id)
        {
            UserId = userId;
            RoleId = roleId;
            AssignedAt = DateTime.UtcNow;
            AssignedBy = assignedBy;
            ExpiresAt = expiresAt;
        }

        /// <summary>
        /// 检查角色是否有效
        /// </summary>
        public virtual bool IsValid()
        {
            return !IsDeleted && !IsExpired;
        }

        /// <summary>
        /// 更新过期时间
        /// </summary>
        public virtual void UpdateExpiration(DateTime? expiresAt)
        {
            ExpiresAt = expiresAt;
        }

        /// <summary>
        /// 立即过期
        /// </summary>
        public virtual void ExpireNow()
        {
            ExpiresAt = DateTime.UtcNow;
        }
    }
}